﻿function addTab(subtitle, url, icon, closable, id) {

    if (!$('#tabs').tabs('exists', subtitle)) {
        $('#tabs').tabs('add', {
            title: subtitle,
            content: createFrame(url, id),
            closable: closable
            , icon: icon
        });
    } else {
        $('#tabs').tabs('select', subtitle);

    }
    tabClose();
}
function createFrame(url, id) {
    var s = '<iframe id="' + id + '" scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:99%;overflow-y: auto; "></iframe>';
    return s;
}

function tabClose() {
    /*双击关闭TAB选项卡*/
    $(".tabs-inner").dblclick(function () {
        var subtitle = $(this).children(".tabs-closable").text();
        $('#tabs').tabs('close', subtitle);
    })
    /*为选项卡绑定右键*/
    $(".tabs-inner").bind('contextmenu', function (e) {
        $('#mm').menu('show', {
            left: e.pageX,
            top: e.pageY
        });

        var subtitle = $(this).children(".tabs-closable").text();

        $('#mm').data("currtab", subtitle);
        $('#tabs').tabs('select', subtitle);
        return false;
    });
}
//绑定右键菜单事件
function tabCloseEven() {
    //刷新
    $('#mm-tabupdate').click(function () {
        var currTab = $('#tabs').tabs('getSelected');
        var url = $(currTab.panel('options').content).attr('src');
        var id = $(currTab.panel('options').content).attr('id');; //获取id

        $('#tabs').tabs('update', {
            tab: currTab,
            options: {
                content: createFrame(url, id)
            }
        })
    })
    //关闭
    $('#mm-tabclose').click(function () {
        var currtab_title = $('#mm').data("currtab");
        $('#tabs').tabs('close', currtab_title);
    })


    //退出
    $("#mm-exit").click(function () {
        $('#mm').menu('hide');
    })
}


$(document).ready(function () {
    var smuser = new SmUser();
    smuser.GetCurrentUser();
    var dt=new Date();
    var y=dt.getFullYear();
    var m=dt.getMonth()+1;
    var d=dt.getDate();
    $("#spnUser").html(smuser.NAME + "，欢迎您的光临！今天是"+y+"年"+m+"月"+d+"日");// "(" + smuser.LERVER +
    $("#loginOut").click(function () {
        $.messager.confirm('系统提示', '您确定要退出本次登录吗?', function (r) {
            if (r) {
                smuser.LogOut();
            }
        });
    });

    tabCloseEven();
    addTab("我的工作台", "map.html", "icon-ok", false);

    var menus = new SmDataTable();
    var sql="select MENU_ID,MENU_NAME,LINK_URL from BASE_menu where PARENT_MENU_ID ='00' and MENU_ID in (select menu_id from BASE_role_menu where role_id in (select role_id from BASE_user_role where user_ID='"+smuser.USER_ID+"')) order by MENU_ID";
    menus.Init(sql);
    
    if (menus != null && menus.RecordCount > 0) {
        $('#divMenus').accordion({
            animate: false
        });
        for (i in menus.Data.m_Rows) {
            //一级菜单，是个panel
            //二级/三级菜单是个tree
            var treehtml="";
            treehtml+="<ul id='menu"+i+"' class='easyui-tree'>";
            treehtml+="</ul>";
            $('#divMenus').accordion('add', {
                title: menus.Data.m_Rows[i][1],
                content: treehtml,
                iconCls: menus.Data.m_Rows[i][3],
                select: false
            });

            GetSmJSON2(CLDJServiceUrl + "/SmwsFrame/GetMenu", "userid=" + smuser.USER_ID + "&bsm=" + menus.Data.m_Rows[i][0], function () {
               if (xmlhttp.readyState==4)
             {// 4 = "loaded"
                if (xmlhttp.status==200)
                 {// 200 = "OK"
                    var txt= parseResponseText(xmlhttp.responseText)
                    $("#menu" + i).tree({ data: JSON.parse(txt) });
                }
                }
            });
            
            $("#menu" + i).tree({
                onClick: function (node) {
                    addTab(node.text, node.attributes.url, node.iconCls, true);
                }
            });
            
        }
        $('#divMenus').accordion({
            animate: true
        });
    }

    $('ul li a').click(function () {
        var tabTitle = $(this).text();
        var url = $(this).attr("rel"); //获取地址
        var id = $(this).attr("id"); //获取id
        var icon = $(this).attr("icon"); //获取图标
        if (icon == "") {
            icon = "icon-save";
        }
        addTab(tabTitle, url, icon, true, id);

    });



});

